﻿module Sustainalytics.EsgRatings {


    export class ESGProfileBackendService {
        private $q: ng.IQService;
        private $http: ng.IHttpService;
        private notificationFactory: INotificationFactory;
        private staticObjects: IStaticObjects;
        private $location: ng.ILocationService;

        constructor($q: ng.IQService, $http: ng.IHttpService, $location: ng.ILocationService, notificationFactory: INotificationFactory, staticObjects: IStaticObjects) {

            var ctx = this;
            ctx.$q = $q;
            ctx.$http = $http;
            ctx.notificationFactory = notificationFactory;
            ctx.staticObjects = staticObjects;
            ctx.$location = $location;
        }

        public loadProfiles(defaultOnly:boolean): ng.IPromise<ESGProfileOutDto> { 
            var ctx = this;

            return ctx.$http.get(ctx.servUrl(), { params: { defaultOnly: defaultOnly } } );
        }

        public saveProfile(dto: ESGProfileDto): ng.IHttpPromise<ESGProfileDto> {
            var ctx = this;
            return ctx.$http.put<ESGProfileDto>(ctx.servUrl(), dto);// replace
        } 


        private servUrl(): string {
            var ctx = this;
            return ctx.$location.protocol() + "://" + ctx.$location.host() + ":" + ctx.$location.port() +
                "/esgratingserv/api/v1/esgprofile/";
        }
    }

    // register service with EsgRatings Angular module
    angular
        .module('Sustainalytics.EsgRatings')
        .factory('ESGProfileBackend',
        ($q: ng.IQService, $http: ng.IHttpService, $location: ng.ILocationService, notificationFactory: INotificationFactory, staticObjects: IStaticObjects) => {

            return new ESGProfileBackendService($q, $http, $location, notificationFactory, staticObjects);            

        });
}